package cn.kgc.commons.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 *MD5工具类
 *
 */
public class MD5Util {
    public static String md5(String origin){
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(origin.getBytes());
            byte[] digest = md.digest();
            // 使用Base64编码
            String base64 = Base64.getEncoder().encodeToString(digest);
            return base64;
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("不可能抛出的异常");
        }
    }

    public static String md5(String origin,String salt) {
        return md5(origin+salt);
    }

    public static String md5(String origin,String salt,int count) {
        for (int i = 0; i < count; i++) {
            origin = md5(origin,salt);
        }
        return origin;
    }


}
